#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        appForm.py
# Author:      KuKei
# Create Date: 2011-10-13
# Description: 应用部分表单
# Modify Date: 2011-10-13

import hashlib 
from django import forms
from sd.cms.forms import BaseForm, BaseModelForm
from sd.cms.models.appModel import *
from sd.cms.models.paramModel import *
from sd.cms.lib.utility import *

class ArticleForm(BaseModelForm):
    contentF  =  forms.CharField(label="内容", required=True)
    #catalog  = forms.ModelChoiceField(label="目录", required=True, queryset = Catalog.objects.filter(level=20).order_by('orderby'), widget=forms.Select)
    #自定义验证
    def clean_contentF(self):
        contentF = self.data['contentF']
        if len(contentF)<20:
            raise forms.ValidationError("内容太少了，请补充!")
        return contentF
    #检验name时候重复
    def clean_name(self):
        name = self.data['name'] if self.data.has_key('name') else ''
        if len(name) >0:
            if not isEng(name.encode('utf-8')) :
                raise forms.ValidationError("地址名只能是字母、数字或下划线！")
            if self.bv.request.POST:
                if Article.objects.filter(name=name).exclude(id=self.id).count() > 0:
                    raise forms.ValidationError("地址名重复，请更改！")
        return name
    
    class Meta:
        model = Article
        fields = ('title','catalog','status','isComment','name','contentF','frontcover')
        widgets = {
            'isComment': forms.CheckboxInput,
            'frontcover': forms.TextInput(attrs={'readonly':'readonly'}),
        }
        
class ArticleCmtForm(BaseModelForm):
    safecode  = forms.CharField(max_length=4, min_length=4, label="验证码", widget=forms.HiddenInput)

    #自定义验证
    def clean_safecode(self):
        safecode = self.data['safecode']
        if self.bv.request.session.has_key(self.bv.params.SAFECODE):
            if hashlib.sha1(safecode).hexdigest() != self.bv.request.session[self.bv.params.SAFECODE]:
                raise forms.ValidationError("验证码不正确!")
        else:
            raise forms.ValidationError("验证码的Session不存在!")
        return safecode
    
    class Meta:
        model = ArticleCmt
        fields = ('pid', 'author', 'email', 'homepage', 'content', 'safecode')
        
class ImageForm(BaseModelForm):
    class Meta:
        model = Image
        fields = ('kind', 'name', 'linkUrl', 'thumbnail','image', 'describe', 'orderby', 'isdisplay')
        widgets = {
            'isdisplay': forms.CheckboxInput,
        }
        
class ProductForm(BaseModelForm):
    #自定义验证
    def clean_content(self):
        content = self.data['content']
        if len(content)<10:
            raise forms.ValidationError("内容太少了，请补充!")
        return content
    #检验name时候重复
    def clean_name(self):
        name = self.data['name'] if self.data.has_key('name') else ''
        if len(name) >0:
            if not isEng(name.encode('utf-8')):
                raise forms.ValidationError("地址名只能是字母、数字或下划线！")
            if self.bv.request.POST > 0:
                if Article.objects.filter(name=name).exclude(id=self.id).count() > 0:
                    raise forms.ValidationError("地址名重复，请更改！")
            return name
    class Meta:
        model = Product
        fields = ('title','catalog','status','name','isComment','isShow','content','frontcover')
        widgets = {
            'frontcover': forms.TextInput(attrs={'readonly':'readonly'}),
            'isComment': forms.CheckboxInput,
            'isShow': forms.CheckboxInput,
        }
        
class ProductParamForm(BaseModelForm):
    class Meta:
        model = ProductParam
        fields = ('title', 'value', 'orderby', 'isdisplay', 'isimportant')
        
class ProductCmtForm(BaseModelForm):
    safecode  = forms.CharField(max_length=4, min_length=4, label="验证码", widget=forms.HiddenInput)

    #自定义验证
    def clean_safecode(self):
        safecode = self.data['safecode']
        if self.bv.request.session.has_key(self.bv.params.SAFECODE):
            if hashlib.sha1(safecode).hexdigest() != self.bv.request.session[self.bv.params.SAFECODE]:
                raise forms.ValidationError("验证码不正确!")
        else:
            raise forms.ValidationError("验证码的Session不存在!")
        return safecode
    
    class Meta:
        model = ArticleCmt
        fields = ('pid', 'author', 'email', 'homepage', 'content', 'safecode')
        
class GuestBookForm(BaseModelForm):
    safecode  = forms.CharField(max_length=4, min_length=4, label="验证码", widget=forms.HiddenInput)

    #自定义验证
    def clean_safecode(self):
        safecode = self.data['safecode']
        if self.bv.request.session.has_key(self.bv.params.SAFECODE):
            if hashlib.sha1(safecode).hexdigest() != self.bv.request.session[self.bv.params.SAFECODE]:
                raise forms.ValidationError("验证码不正确!")
        else:
            raise forms.ValidationError("验证码的Session不存在!")
        return safecode
    
    class Meta:
        model = GuestBook
        fields = ('title', 'authorname', 'email', 'message', 'safecode', 'reply')